﻿/*阿里云图片存储类
 * daneel  
 * 2019-01-02
 * 
 * */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Aliyun.OSS;
using Aliyun.OSS.Common;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Business
{
    /// <summary>
    /// 阿里云图片存储服务
    /// </summary>
    public static class AliyunOssBiz
    {
        static string accessKeyId = ConfigHelper.AppSetting<string>("aliyunoss_AccessKeyId", AliyunOssConfig.AccessKeyId); 
        static string accessKeySecret = ConfigHelper.AppSetting<string>("aliyunoss_AccessKeySecret", AliyunOssConfig.AccessKeySecret); 
        static string endpoint = ConfigHelper.AppSetting<string>("aliyunoss_endpoint", "oss-cn-shanghai.aliyuncs.com");
        static OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
        static string bucketName = ConfigHelper.AppSetting<string>("aliyunoss_goodsimg_bucketname", "daneel-img");
        static string bucketNameAppk = ConfigHelper.AppSetting<string>("aliyunoss_apk_bucketname", "daneel-apk");

        /// <summary>
        /// 上传图片使用本地地址
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filePathToUpload"></param>
        /// <returns></returns>
        public static Tuple<bool, string> PutGoodsImgDir(string key, string filePathToUpload)
        {
            var result = PutObjectWithDir(bucketName, key, filePathToUpload);
            if (string.IsNullOrWhiteSpace(result))
                return new Tuple<bool, string>(false, "上传失败");
            return new Tuple<bool, string>(true, key);
        }

        /// <summary>
        /// 上传图片Stream
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filePathToUpload"></param>
        /// <returns></returns>
        public static Tuple<bool, string> PutGoodsImgStream(string key, Stream content)
        {
            var result = PutObjectWithStream(bucketName, key, content);
            if (string.IsNullOrWhiteSpace(result))
                return new Tuple<bool, string>(false, "上传失败");
            return new Tuple<bool, string>(true, key);
        }

        public static string GetImg(string key, string imgstyle = "")
        {
            if (string.IsNullOrWhiteSpace(imgstyle))
                return $"https://{bucketName}.{endpoint}/{key}";
            else
                return $"https://{bucketName}.{endpoint}/{key}?x-oss-process=style/{imgstyle}";
        }

        public static string GetImg_small(string key)
        {
            string imgstyle = "small";
            return $"https://{bucketName}.{endpoint}/{key}?x-oss-process=style/{imgstyle}";
        }
        public static string GetImg_60x60(string key)
        {
            string imgstyle = "60x60";
            return $"https://{bucketName}.{endpoint}/{key}?x-oss-process=style/{imgstyle}";
        }
        /// <summary>
        /// 使用给本地路径上传
        /// </summary>
        /// <param name="bucketName"></param>
        public static string PutObjectWithDir(string bucketName, string key, string filePathToUpload)
        {
            try
            {
                var putResult = client.PutObject(bucketName, key, filePathToUpload);
            }
            catch (OssException ex)
            {
                LogHelper.Error("上传失败1，" + ex.GetExcetionMessage());
                return "";
            }
            catch (Exception ex)
            {
                LogHelper.Error("上传失败2，" + ex.GetExcetionMessage());
                return "";
            }
            return key;
        }
        /// <summary>
        /// 使用内存上传
        /// </summary>
        /// <param name="bucketName"></param>
        public static string PutObjectWithStream(string bucketName, string key, Stream content)
        {
            try
            {
                var putResult = client.PutObject(bucketName, key, content);
            }
            catch (OssException ex)
            {
                LogHelper.Error("上传失败1，" + ex.GetExcetionMessage());
                return "";
            }
            catch (Exception ex)
            {
                LogHelper.Error("上传失败2，" + ex.GetExcetionMessage());
                return "";
            }
            return key;
        }
        public static string GetFile(string bucketName, string key)
        {
            return $"https://{bucketName}.{endpoint}/{key}";
        }

    }
}
